package com.green.web.controller;

import com.green.web.entity.Address;
import com.green.web.entity.User;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 参数接收
 */
@RestController
@RequestMapping("/test3")
public class Test3Controller {


    /**
     * 通过HttpServletRequest接收form表单和地址上数据，接收不到body中的数据
     * http://localhost:8080/test3/method1?age=18&name=zhangsan
     * @param request
     * @return
     */
    @RequestMapping("/method1")
    public String method1(HttpServletRequest request){
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        return "name:"+name+" age:"+age;
    }


    /**
     * 通过形参接收数据,RequestParam默认是必填
     * http://localhost:8080/test3/method2?name=lisi
     * @param request
     * @return
     */
    @RequestMapping("/method2")
    public String method2(@RequestParam(value = "name") String name,
                          @RequestParam(value = "age",defaultValue = "10") Integer age){
        return "name:"+name+" age:"+age;
    }

    /**
     * 参数在地址栏上或form表单中，属性要跟实体中的属性名一致才行，通过实体类接收，
     * http://localhost:8080/test3/method3?name=lisi&zipCode=123
     * @param address
     * @return
     */
    @RequestMapping("/method3")
    public String method3(Address address){
        return "name:"+address.getName()+" ZipCode:"+address.getZipCode();
    }


    /**
     * 参数在地址栏上或form表单中，数组元素以逗号隔开
     * http://localhost:8080/test3/method4?balls=篮球,足球,乒乓球
     * http://localhost:8080/test3/method4?balls=篮球&balls=足球
     * @param address
     * @return
     */
    @RequestMapping("/method4")
    public String method4(@RequestParam(value = "balls") String[] balls){
        return String.join("|", balls);
    }


    /**
     * 参数在地址栏上或form表单中，数组元素以逗号隔开
     * http://localhost:8080/test3/method5?balls=篮球,足球,乒乓球
     * @param address
     * @return
     */
    @RequestMapping("/method5")
    public String method5(@RequestParam(value = "balls") List<String> balls){
        return String.join("==", balls);
    }


    /**
     * 从请求体中获取数据
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/method7")
    public String method7(HttpServletRequest request) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    }

    /**
     * 参数在body中
     * {
     *     "id":1,
     *     "name":"zhangsan",
     *     "age":14,
     *     "jobs":["程序员","送外卖"],
     *     "address":{
     *         "name":"小新塘",
     *         "zipCode":"1234"
     *     },
     *     "time":"2024-08-01 12:00:11"
     * }
     * @param address
     * @return
     */
    @RequestMapping("/method6")
    public String method6(@RequestBody User user){
        return user.toString();
    }


    /**
     * 跟method6一样
     * @param map
     * @return
     */
    @RequestMapping("/method8")
    public Map method8(@RequestBody Map<String, Object> map){
        return map;
    }



    /**
     * 参数在地址上
     * http://localhost:8080/test3/method9/123
     * @param map
     * @return
     */
    @RequestMapping("/method9/{id}")
    public String method9(@PathVariable(value = "id") Integer id){
        return "id:"+id;
    }



}
